/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

XiaoNuo采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改XiaoNuo源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
6.若您的项目无法满足以上几点，可申请商业授权，获取XiaoNuo商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.cn.xiaonuo.slbfinances.attractinfo.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.utils.DateUtils;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.attractinfo.entity.SlAttractInfoSet;
import com.cn.xiaonuo.slbfinances.attractinfo.enums.SlAttractInfoSetExceptionEnum;
import com.cn.xiaonuo.slbfinances.attractinfo.mapper.SlAttractInfoSetMapper;
import com.cn.xiaonuo.slbfinances.attractinfo.param.SlAttractInfoSetParam;
import com.cn.xiaonuo.slbfinances.attractinfo.service.SlAttractInfoSetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 招商列表
 *
 * @author 卢阳阳
 * @date 2021-03-08 14:55:13
 */
@Service
public class SlAttractInfoSetServiceImpl extends ServiceImpl<SlAttractInfoSetMapper, SlAttractInfoSet> implements SlAttractInfoSetService {

    @Resource
    private SlAttractInfoSetMapper slAttractInfoSetMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public PageResult<SlAttractInfoSet> page(SlAttractInfoSetParam slAttractInfoSetParam) {
        if(slAttractInfoSetParam.getShopId()==null){
            throw new ServiceException(SlAttractInfoSetExceptionEnum.SHOP_EXIST);
        }
        QueryWrapper<SlAttractInfoSet> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(slAttractInfoSetParam)) {

            // 根据姓名 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractInfoSetParam.getRealName())) {
                queryWrapper.lambda().like(SlAttractInfoSet::getRealName, slAttractInfoSetParam.getRealName());
            }

            // 根据电话 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractInfoSetParam.getPhone())) {
                queryWrapper.lambda().eq(SlAttractInfoSet::getPhone, slAttractInfoSetParam.getPhone());
            }
            // 根据商铺id
            if (ObjectUtil.isNotEmpty(slAttractInfoSetParam.getShopId())) {
                queryWrapper.lambda().eq(SlAttractInfoSet::getShopId, slAttractInfoSetParam.getShopId());
            }
        }
        queryWrapper.lambda().eq(SlAttractInfoSet::getOperateStatus, 1);//查询出正常的招商信息
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public List<SlAttractInfoSet> list(SlAttractInfoSetParam slAttractInfoSetParam) {
        LambdaQueryWrapper<SlAttractInfoSet> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(slAttractInfoSetParam)) {
            // 根据姓名 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractInfoSetParam.getRealName())) {
                queryWrapper.like(SlAttractInfoSet::getRealName, slAttractInfoSetParam.getRealName());
            }

            // 根据电话 查询
            if (ObjectUtil.isNotEmpty(slAttractInfoSetParam.getPhone())) {
                queryWrapper.eq(SlAttractInfoSet::getPhone, slAttractInfoSetParam.getPhone());
            }
            // 根据商家id查询
            if (slAttractInfoSetParam.getShopId()!=null) {
                queryWrapper.eq(SlAttractInfoSet::getShopId, slAttractInfoSetParam.getShopId());
            }
        }
        queryWrapper.eq(SlAttractInfoSet::getOperateStatus, 1);//查询出正常的招商信息
        return this.list(queryWrapper);
    }

    @DataSource(name = "jyb_soole_data_hn")
    public boolean isExistPhone(String phone) {
        boolean flag=false;
        LambdaQueryWrapper<SlAttractInfoSet> queryWrapper = new LambdaQueryWrapper<>();
            // 根据电话 模糊查询
            if (StringUtils.isBlank(phone)){
                return flag;
            }
        queryWrapper.eq(SlAttractInfoSet::getPhone,phone);
        queryWrapper.eq(SlAttractInfoSet::getOperateStatus, 1);//查询出正常的招商信息
        List<SlAttractInfoSet> list= this.list(queryWrapper);
        if(list!=null){
            if(list.size()>0){
                flag=true;
            }
        }
        return flag;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void add(SlAttractInfoSetParam slAttractInfoSetParam) {
        SlAttractInfoSetParam slAttractInfo=new SlAttractInfoSetParam();
        slAttractInfo.setShopId(slAttractInfoSetParam.getShopId());
        /*List<SlAttractInfoSet> listByPhone=this.list(slAttractInfo);
        if(listByPhone!=null){
            if(listByPhone.size()>0){
                throw new ServiceException(SlAttractInfoSetExceptionEnum.PHONE_EXIST);
            }
        }*/
        List<SlAttractInfoSet> list=this.list(slAttractInfo);
        if(list!=null){
            if(list.size()>3){
                throw new ServiceException(SlAttractInfoSetExceptionEnum.MAX_NUM_EXIST);
            }
        }
        SlAttractInfoSet slAttractInfoSet=new SlAttractInfoSet();
        BeanUtil.copyProperties(slAttractInfoSetParam, slAttractInfoSet);
        slAttractInfoSet.setCreateTime(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        slAttractInfoSet.setOperateStatus(1);//正常
        this.save(slAttractInfoSet);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void delete(SlAttractInfoSetParam slAttractInfoSetParam) {
        SlAttractInfoSet slAttractInfoSet = this.getById(slAttractInfoSetParam.getId());
        if (ObjectUtil.isNull(slAttractInfoSet)) {
            throw new ServiceException(SlAttractInfoSetExceptionEnum.NOT_EXIST);
        }else{
            slAttractInfoSet.setUpdateTime(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
            slAttractInfoSet.setOperateStatus(2);//删除
            this.updateById(slAttractInfoSet);
        }
       // this.removeById(SlAttractInfoSetParam.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void edit(SlAttractInfoSetParam slAttractInfoSetParam) {
       /* boolean flag=isExistPhone(slAttractInfoSetParam.getPhone());
        if(flag){
            throw new ServiceException(SlAttractInfoSetExceptionEnum.PHONE_EXIST);
        }*/
        SlAttractInfoSet slAttractInfoSet = this.querySlAttractInfoSet(slAttractInfoSetParam);
        BeanUtil.copyProperties(slAttractInfoSetParam, slAttractInfoSet);
        slAttractInfoSet.setUpdateTime(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));//时间更新
        this.updateById(slAttractInfoSet);
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public SlAttractInfoSet detail(SlAttractInfoSetParam SlAttractInfoSetParam) {
        return this.querySlAttractInfoSet(SlAttractInfoSetParam);
    }

    /**
     * 获取SLB店铺
     *
     * @author 卢阳阳
     * @date 2021-03-08 14:55:13
     */
    @DataSource(name = "jyb_soole_data_hn")
    private SlAttractInfoSet querySlAttractInfoSet(SlAttractInfoSetParam SlAttractInfoSetParam) {
        SlAttractInfoSet SlAttractInfoSet = this.getById(SlAttractInfoSetParam.getId());
        if (ObjectUtil.isNull(SlAttractInfoSet)) {
            throw new ServiceException(SlAttractInfoSetExceptionEnum.NOT_EXIST);
        }
        return SlAttractInfoSet;
    }
}

